package com.mkvld.appietijdenv2.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mkvld.appietijdenv2.databases.ShiftDatabase;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

public class Backup {
	private static final String[] PREF_BACKUP_KEYS = {
		"pref_contract", "pref_loan", "budget_period", "budget_year",
		"budget_vacation_budget", "budget_vacation_breeded", "budget_vacation_withdrawn", "budget_vacation_nextyear",
		"budget_adv_budget", "budget_adv_breeded", "budget_adv_withdrawn",
		"budget_tvt_budget", "budget_tvt_breeded", "budget_tvt_withdrawn"
	};
	public static boolean backup(Context context) {
		
		List<Serializable> items = new ArrayList<Serializable>();
		List<Shift> shifts = ShiftDatabase.db.getShifts();
		for (Shift shift : shifts) {
			items.add(shift);
		}
		SharedPreferences prefs = Preferences.get(context);
		for (String key : PREF_BACKUP_KEYS) {
			items.add(new Pref(key, prefs.getString(key, null)));
		}
		File path = new File(Environment.getExternalStorageDirectory(), "appietijden-backup");
		path.mkdir();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddkkmmss", Utils.locale);
			
			File file = new File(path, sdf.format(new Date()));
			file.createNewFile();
			
			FileOutputStream fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(items);
			oos.close();
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void restore(Context context, File file) {
		try {
			FileInputStream fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			List<Serializable> items = (List<Serializable>)ois.readObject();
			ois.close();
			//ShiftDatabase.clear(context);
			SharedPreferences.Editor prefs = Preferences.get(context).edit();
			for (Serializable item : items) {
				if (item.getClass() == Shift.class)
					ShiftDatabase.db.addShift((Shift)item);
				else if (item.getClass() == Pref.class)
					prefs.putString(((Pref)item).key, ((Pref)item).value);
			}
			prefs.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static File[] getFiles(Context context) {
		File path = new File(Environment.getExternalStorageDirectory(), "appietijden-backup");
		File[] files = path.listFiles(new FilenameFilter() {
			
			@Override
			public boolean accept(File dir, String filename) {
				if (filename.equals("tijden"))
					return true;
				try {
					SimpleDateFormat sdf = new SimpleDateFormat("yyMMddkkmmss", Utils.locale);
					sdf.parse(filename);
					return true;
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				} 
			}
		});
		if (files == null)
			files = new File[0];
		return files;
	}
	
	public static class Pref implements Serializable {
		private static final long serialVersionUID = 7136493218058554288L;
		public Pref(String key, String value) {
			this.key = key;
			this.value = value;
		}
		String key, value;
	}
}
